package controller;

import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import dao.XxDao;
import model.User;

/**
 * Servlet implementation class addUserServlet
 */
@WebServlet("/XxFdytAddServlet")
public class XxFdytAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
        String classid = request.getParameter("class");
//     测试
        System.out.println("AddWeiGuiServlet id:"+id+"  time:"+classid);
//        User use = new User();
        XxDao dao = new XxDao();
        User use =dao.findnamepsw(id);
        String name = use.getName();
        String password = use.getPassword();
        boolean tip = dao.addFdyXx(id, name,password, classid);
    	

        // 如果添加用户成功，转发到userlList.jsp，列出所有用户
        if (tip) {
            // 验证成功，转发到 welcome.jsp
        	response.sendRedirect("View_xx/XxFdy.jsp");
            
        } else {
            // 验证失败，重新回到 login.jsp
            response.sendRedirect("View_xx/XxFdy.jsp?error=sameclass");
        }
	}
}
